有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java集合。shuffle未按预期工作

我正在尝试编写一个简单的加密算法,它是一种随机移位模式加密。我写它是基于所有字符的数组,然后使用集合。洗牌,然后把东西和它配对。但是,由于输出文本与输入文本相同,所以数组似乎没有乱序

这是我的方法

static void encrypt(String s)
    {
        //Define variable for indexOf output
        int n;

        //Encrypted output
        String output = "";

        //Shuffle array to create random replacements
        Collections.shuffle(Arrays.asList(alphashuff));

        //Create new string
        String alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

        for (int index = 0; index < s.length();index++)
        {
            char aChar = s.charAt(index);
            n = alpha.indexOf(aChar, 0);

            if(n == -1)
            {
                output = output + aChar;
            }
            else
            {
                output = output + alphashuff[n];
            }

        }

        //Print encrypted string to console
        System.out.println("Encrypted Text: " + output);
    }

共 (0) 个答案